package com.todod.service;

import java.io.IOException;
import java.time.LocalDateTime;
import java.util.List;

import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.todod.common.util.SnowflakeIdGenerator;
import com.todod.dto.EsFilesDto;
import com.todod.mapper.PublicFileMapper;
import com.todod.mapper.PublicFileQuantityMapper;
import com.todod.model.PublicFile;
import com.todod.model.PublicFileQuantity;

import net.logstash.logback.encoder.org.apache.commons.lang.StringUtils;

@Service
@Transactional
public class PublicFileQuantityService {

	@Autowired
	private PublicFileQuantityMapper publicFileQuantityMapper;
	@Autowired
	private RestHighLevelClient restHighLevelClient;
	@Autowired
	private PublicFileMapper publicFileMapper;
	@Autowired
	private PublicFileService publicFileService;

	/**
	 * 
	 * @param _userId
	 * @param publicFileQuantity
	 * @param value
	 * @param value2
	 */
	public void saveData(Long userId, Long fileId, String date, String downloads, String views) {

		PublicFileQuantity publicFileQuantity = new PublicFileQuantity();
		publicFileQuantity.setId(SnowflakeIdGenerator.getId());
		publicFileQuantity.setFileId(fileId);
		publicFileQuantity.setDate(LocalDateTime.now().toString().substring(0, 10));

		if (downloads != null && StringUtils.isNotBlank(downloads)) {
			publicFileQuantity.setDownloads(1);
		} else {
			publicFileQuantity.setDownloads(0);
		}
		if (views != null && StringUtils.isNotBlank(views)) {
			publicFileQuantity.setViews(1);
		} else {
			publicFileQuantity.setViews(0);
		}

		publicFileQuantity.setCreatetimeTi(LocalDateTime.now());
		publicFileQuantity.setCreateuserId(userId);
		publicFileQuantityMapper.insert(publicFileQuantity);

		insertDoc(publicFileQuantity, fileId);
	}

	/**
	 * 
	 * @param _userId
	 * @param projectInfo
	 * @param value
	 * @param value2
	 */
	public void editData(Long _userId, Long fileId, String date, String downloads, String views) {

		PublicFileQuantity publicFileQuantity1 = getEvaluateListByFileId(fileId, date);

		if (downloads != null && StringUtils.isNotBlank(downloads)) {
			publicFileQuantity1.setDownloads(publicFileQuantity1.getDownloads() + 1);
		}

		if (views != null && StringUtils.isNotBlank(views)) {
			publicFileQuantity1.setViews(publicFileQuantity1.getViews() + 1);
		}

		publicFileQuantity1.setModifytimeTi(LocalDateTime.now());
		publicFileQuantity1.setModifyuserId(_userId);
		publicFileQuantityMapper.updateById(publicFileQuantity1);

		insertDoc(publicFileQuantity1, fileId);
	}

	/**
	 * 
	 * @param code
	 * @return
	 */
	public List<PublicFileQuantity> getEvaluateListByFileId(Long fileId) {
		QueryWrapper<PublicFileQuantity> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("gfq_file_mid", fileId);

		List<PublicFileQuantity> list = publicFileQuantityMapper.selectList(queryWrapper);

		return list;
	}

	/**
	 * 
	 * @param code
	 * @return
	 */
	public PublicFileQuantity getEvaluateListByFileId(Long fileId, String date) {
		QueryWrapper<PublicFileQuantity> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("gfq_file_mid", fileId);
		queryWrapper.eq("gfq_date", date);

		return publicFileQuantityMapper.selectOne(queryWrapper);
	}

	public boolean getEvaluateListByDate(Long fileId, String date) {
		QueryWrapper<PublicFileQuantity> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("gfq_file_mid", fileId);
		queryWrapper.eq("gfq_date", date);

		List<PublicFileQuantity> list = publicFileQuantityMapper.selectList(queryWrapper);
		if (list.size() > 0) {
			return false;
		} else {
			return true;
		}
	}

	private void insertDoc(PublicFileQuantity publicFileQuantity, Long fileId) {

		EsFilesDto esFilesDto = new EsFilesDto();

		esFilesDto.setId(publicFileQuantity.getId());
		esFilesDto.setFileId(fileId);
		esFilesDto.setDate(publicFileQuantity.getDate());
		esFilesDto.setDownloads(publicFileQuantity.getDownloads());
		esFilesDto.setViews(publicFileQuantity.getViews());

		BulkRequest bulkRequest = new BulkRequest();
		bulkRequest.add(new IndexRequest("ckzx").id(String.valueOf(publicFileQuantity.getId()))
				.source(JSON.toJSONString(esFilesDto), XContentType.JSON));

		try {
			BulkResponse bulkResponse = restHighLevelClient.bulk(bulkRequest, RequestOptions.DEFAULT);
			if (bulkResponse.hasFailures()) {
				System.out.println(bulkResponse.buildFailureMessage());
			}

			PublicFile publicFile = publicFileMapper.selectById(fileId);
			publicFileService.insertDoc(publicFile, "edit");
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public List<PublicFileQuantity> getSortByDate(String format) {
		QueryWrapper<PublicFileQuantity> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("gfq_date", format);

		List<PublicFileQuantity> list = publicFileQuantityMapper.selectList(queryWrapper);

		return list;
	}

}
